//
//  NewsCell.m
//  BosidengIphone
//
//  Created by  on 11-12-29.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "NewsCell.h"
#import "AppDelegate.h"
#import "NewsViewController.h"

@implementation NewsCell

@synthesize dateView;
@synthesize picture;
@synthesize title;
@synthesize text;
@synthesize morePicture;

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        
    }
    return self;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        //
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [morePicture addTarget:self action:@selector(showMorePicture:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)dealloc {
    [dateView release];
    [picture release];
    [title release];
    [text release];
    [morePicture release];
    [super dealloc];
}

- (IBAction)showMorePicture:(id)sender {
    [mViewControll jumpNewsImagesView:mData];
}

- (void)config:(id)viewController data:(NewsData *)data indexPath:(NSIndexPath *)indexPath {
    mViewControll = viewController;
    mData = data;
    int y = 0;
    dateView.text = data.publishDate;
    y += 24;
    
    if ([data hasSLImage]) {
        NSString *imageUrl = data.iPhoneSLImg;
        NSMutableDictionary *imageData = data.noSaveData;
        UIImage *image = [imageData objectForKey:imageUrl];
        if (image == nil) {
            NSString *imagePath = [AppDelegate getCacheFilePathByUrl:imageUrl];
            if ([AppDelegate checkFileExist:imagePath]) {
                //NSLog(@"load from file");
                image = [UIImage imageWithContentsOfFile:imagePath];
                [imageData setValue:image forKey:imageUrl];
            } else {
                [viewController requestNewsImage:imageUrl localPath:imagePath indexPath:indexPath];
            }
        } else {
            //NSLog(@"image load from buffer");
        }
        int height = [data.imageHeight intValue]/2;
        if (image != nil) {
            picture.image = image;
            height = image.size.height/2;
            picture.frame = CGRectMake(0, y, 320, height);
            picture.hidden = NO;
        } else {
            picture.hidden = YES;
        }
        //NSLog(@"image view height=%d", height);
        y += height;
    } else {
        picture.hidden = YES;
    }
    y+=5;
    
    CGRect frame = title.frame;
    title.frame = CGRectMake(10, y, 300, 22);
    title.text = data.title;
    y+=frame.size.height+5;
    
    //frame = text.frame;
    text.text = data.content;
    CGFloat height = [data getContentHeight];
    CGFloat lineHeight = [UIFont systemFontOfSize:10].lineHeight;
	text.numberOfLines = height/lineHeight;
    text.frame = CGRectMake(10, y, 300, height);
    y+=height+5;
    
    if ([data hasNewsImgs]) {
        morePicture.frame = CGRectMake(239, y-9, 81, 36);
        //morePicture.backgroundColor = [UIColor redColor];
        morePicture.hidden = NO;
    } else {
        morePicture.hidden = YES;
    }
}

@end
